<?php

    class User extends MY_Controller
    {
        public function __construct()
        {
            parent::__construct();
            $this->load->model('admin/user_model');
        }
        public function index()
        {
            $data=$this->user_model->getAll(10,0,-1);
            $this->load->vars('item',$data);
        }
        
        //add new user
        public function add()
        {
            
        }
        
        //edit user 
        public function edit()
        {
            $id=$this->uri->segment(4);
            $this->data['item']=$this->user_model->getUserById($id);
            $this->load->vars('item',$this->data['item']);
        }
        
        //save new user or update 
        public function save()
        {
           if ($this->form_validation->run() == FALSE)
            {
                $this->form_validation->set_error_delimiters('<div class="error" style="text-align:center;">', '</div>');
                $this->load->view('admin/form/form');
            }
            else
            {
                $post=$this->input->post();
                $id=$post['id'];
                $name=$post['name'];
                $username=$post['username'];
                $password=$post['password'];
                $repassword=$post['repassword'];
                $email=$post['email'];
                $phone=$post['phone'];
                $publish=$post['publish'];
                $order=$post['order'];
                $data=array();
                $data['user_fullname']=$name;
                $data['user_name']=$username;
                $data['user_email']=$email;
                $data['user_phone']=$phone;
                $data['user_order']=$order;
                $data['user_publish']=$publish;
                if($id>0)
                {
                    if($password!="")
                    {
                        $data['user_password']=$password;
                    }
                }
                //call save
                if($this->user_model->save($id,$data)>0)
                {
                    
                    //redirect(base_url()."admin/user");
                }
                
            //}// End if formvalidation
            redirect(base_url()."admin/user");
        }
    }
        //publish user
        public function publish()
        {
            $id=$this->uri->segment(4);
            $this->user_model->publish(1,array($id));
            redirect(base_url()."admin/user");
        }
        //unpublish
        public function unpublish()
        {
            $id=$this->uri->segment(4);
            $this->user_model->publish(0,array($id));
            redirect(base_url()."admin/user");
        }
        //for actions from menu
        
        public function action()
        {
            $task=$this->input->post('task');
            $task=trim($task);
            $id=$this->input->post('id');
            switch($task)
            {
                case 'saveorder':
                $cid=$this->input->post('cid');
                $order=$this->input->post('order');
                $this->user_model->saveorder($cid,$order);
                break;
                case 'delete':
                $this->user_model->delete($id);
                break;
                case 'publish':
                $this->user_model->publish(1,$id);
                break;
                case 'unpublish':
                $this->user_model->publish(0,$id);
                break;
                
            }
            redirect(base_url()."admin/user");
        }
    }